// AddressPanel.vue
<script setup lang="ts">

import { getMemberAddressAPI, postEditAddressAPI } from '@/services/address';
import { useMemberStore } from '@/stores';
import type { AddressItem } from '@/types/address';
import { ref } from 'vue';
const memberStore = useMemberStore()
const addressList = ref<AddressItem[]>()
//获取地址数据
const getAddressList = async () => {
  addressList.value = []
  if (memberStore.profile?.token) {
    const res = await getMemberAddressAPI(memberStore.profile.id)
    addressList.value = res.data
  }
}
const props = defineProps<{
  getAddress: any
}>()
const addressData = ref()
//设置为默认地址
const toDefault = async (item: AddressItem) => {
  item = Object.assign(item, { isDefault: true })
  const res = await postEditAddressAPI(item._id, item)
  addressData.value = res.data.fullLocation + res.data.address
  if (res.code === 2000) {

    uni.showToast({
      title: '设置成功',
      icon: 'success'
    })
    props.getAddress(addressData.value)
    getAddressList()
    emit('close')
  }
}
//跳转到地址页
const toAddress = () => {
  uni.navigateTo({ url: '/pagesMember/address/index' })
}
getAddressList()
//子组件调用父组件
const emit = defineEmits<{
  (event: 'close'): void
}>()
</script>

<template>
  <view class="address-panel">
    <!-- 关闭按钮 -->
    <text class="close icon-close" @tap="emit('close')"></text>
    <!-- 标题 -->
    <view class="title">配送至</view>
    <!-- 内容 -->
    <view class="content" v-if="!memberStore.profile?.token">
      <view class="item">
        <view class="user">李明 13824686868</view>
        <view class="address">北京市顺义区后沙峪地区安平北街6号院</view>
        <text class="icon icon-checked"></text>
      </view>
      <view class="item">
        <view class="user">王东 13824686868</view>
        <view class="address">北京市顺义区后沙峪地区安平北街6号院</view>
        <text class="icon icon-ring"></text>
      </view>
      <view class="item">
        <view class="user">张三 13824686868</view>
        <view class="address">北京市朝阳区孙河安平北街6号院</view>
        <text class="icon icon-ring"></text>
      </view>
    </view>
    <view class="content" v-else>
      <view class="item" v-for="item in  addressList" :key="item._id" @tap="toDefault(item)">
        <view>
          <view class="user">{{ item.receiver + item.telephone }}</view>
          <view class="address">{{ item.fullLocation + item.address }}</view>
        </view>
        <!-- <text class="icon" :class="`${item.isDefault ? 'icon-checked' : 'icon-'}`"></text> -->
        <text>
          <uni-icons :type="`${item.isDefault ? 'checkbox-filled' : ''}`" color="#27ba9b" size="40rpx" />
        </text>
      </view>
    </view>
    <view class="footer">
      <view class="button primary" @tap="toAddress"> 新建地址 </view>
      <view v-if="false" class="button primary">确定</view>
    </view>
  </view>
</template>

<style lang="scss">
.address-panel {
  padding: 0 30rpx;
  border-radius: 10rpx 10rpx 0 0;
  position: relative;
  background-color: #fff;
}

.title {
  line-height: 1;
  padding: 40rpx 0;
  text-align: center;
  font-size: 32rpx;
  font-weight: normal;
  border-bottom: 1rpx solid #ddd;
  color: #444;
}

.close {
  position: absolute;
  right: 24rpx;
  top: 24rpx;
}

.content {
  min-height: 300rpx;
  max-height: 440rpx;
  overflow: scroll;
  padding: 20rpx;
  // border-bottom: #666;

  .item {
    border-bottom: 2rpx solid #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx 50rpx 30rpx 60rpx;
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
    position: relative;
  }

  .icon {
    color: #999;
    font-size: 40rpx;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 0;
  }

  .icon-checked {
    color: #27ba9b;
  }

  .icon-ring {
    color: #444;
  }

  .user {
    font-size: 28rpx;
    color: #444;
    font-weight: 500;
  }

  .address {
    font-size: 26rpx;
    color: #666;
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  .button {
    flex: 1;
    height: 72rpx;
    text-align: center;
    line-height: 72rpx;
    margin: 0 20rpx;
    color: #fff;
    border-radius: 72rpx;
  }

  .primary {
    color: #fff;
    background-color: #27ba9b;
  }

  .secondary {
    background-color: #ffa868;
  }
}
</style>
